package com.happy.z_open.config.common;

import java.io.Serializable;
import java.util.Enumeration;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.commonService.api.message.SendMessageService;
import com.userService.api.user.UserBaseInfoService;
import com.userService.entity.user.UserBaseInfo;
import com.woniu.common.exception.BusinessException;
import com.woniu.common.exception.ExceptionEnum;
import com.woniu.common.token.TokenUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@RestController
public class Common {

	@Reference
	UserBaseInfoService userBaseInfoService;

	@Reference
	SendMessageService sendMessageService;

	@Autowired
	RedisTemplate<String, Serializable> serializableRedisTemplate;
	
	/**
	 * 获取当前用户信息
	 * 
	 * @param request
	 * @return
	 * @throws BusinessException 
	 */
	public UserBaseInfo queryCurrentUserInfo(HttpServletRequest request) throws BusinessException {
		log.info("获取当前登录用户信息:{}", request);
		// 获取请求中的token
		String token = null;
		Enumeration<String> headerNames = request.getHeaderNames();
		for (Enumeration<String> e = headerNames; e.hasMoreElements();) {
			String thisName = e.nextElement().toString();
			if ("accesstoken".equals(thisName)) {
				token = request.getHeader(thisName);
				break;
			}
		}
		log.info("请求中的accesstoken信息：{}", token);
		// 根据token获取用户信息
		String userId = TokenUtil.getAppUID(token);
		log.info("token解密结果-用户信息id：{}", userId);
		if (StringUtils.isEmpty(userId)) {
			throw new BusinessException(ExceptionEnum.TOKEN_USERID_EMPTY);
		} else {
			return this.queryCurrentUserInfo(userId);
		}
	}
	
	/**
	 * 获取当前没缓存 用户信息
	 * 
	 * @param request
	 * @return
	 */
	public UserBaseInfo currentUserInfo(String userInfoId) {
		log.info("获取当前登录用户信息:{}", userInfoId);
		UserBaseInfo userInfo = new UserBaseInfo();
		// 如果缓存中没有，则查询数据库
		log.info("------从数据库获取当前用户信息-----");
		userInfo = userBaseInfoService.queryInfById(userInfoId);
		return userInfo;
	}

	/**
	 * 获取当前用户信息
	 * @param request
	 * @return
	 */
	public UserBaseInfo queryCurrentUserInfo(String userInfoId) {
		log.info("获取当前登录用户信息:{}", userInfoId);
		UserBaseInfo userInfo = new UserBaseInfo();
		// 查询缓存中的用户信息
		Object redisUserInfo = serializableRedisTemplate.opsForValue().get(userInfoId + ":userInfo");
		log.info("------获取当前用户信息结果：{}-----", redisUserInfo);
		if (redisUserInfo != null) {
			userInfo = (UserBaseInfo) redisUserInfo;
		} else {
			// 如果缓存中没有，则查询数据库
			log.info("------从数据库获取当前用户信息-----");
			userInfo = userBaseInfoService.queryInfById(userInfoId);
			// 将查询的用户信息保村存到缓存中，默认7天
			log.info("------从数据库获取当前用户信息结果：{}-----", userInfo);
			serializableRedisTemplate.opsForValue().set(userInfoId + ":userInfo", userInfo, 7, TimeUnit.DAYS);
		}
		return userInfo;
	}

	

}
